package com.cheng.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import static com.cheng.utils.HttpClientUtil.postClient;

/**
 * Author: Cheng
 * Date: 2019/7/19
 * Time: 0:06
 * Description: No Description
 */
@Component
public class TiebaSign {

    private static final Logger logger = LoggerFactory.getLogger(TiebaSign.class);

    private PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

    /**
     * 验证bduss 通过返回tbs
     *
     * @param bduss 贴吧验签
     * @return
     * @throws Exception
     */
    public String verification(String bduss) throws Exception {
        cm.setMaxTotal(200);             //    设置最大连接数
        cm.setDefaultMaxPerRoute(10);    //    设置每个主机的并发数
        JSONObject tbs = (JSONObject) JSON.parse(getTbs(bduss, cm));
        if (!tbs.getString("is_login").equals("1")) {
            return null;
        }
        return tbs.getString("tbs");
    }

    /**
     * 获取贴吧状态码 tbs
     */
    public String getTbs(String bduss, PoolingHttpClientConnectionManager cm) throws Exception {
        String header = "Cookie=" + "BDUSS=" + bduss;
        return postClient("http://tieba.baidu.com/dc/common/tbs", header, "", cm).getResponseStr();
    }

    /**
     * 获取所有关注贴吧
     */
    public String getForums(String bduss, PoolingHttpClientConnectionManager cm, int pageNum, int pageSize) throws Exception {
        String header = "Cookie=BDUSS=" + bduss + "&" +
                "Content-Type=application/x-www-form-urlencoded&Charset=UTF-8&net=3&" +
                "User-Agent=bdtb for Android 8.4.0.1&Connection=Keep-Alive&Accept-Encoding=gzip&" +
                "Host=c.tieba.baidu.com";
        String md5Hex = DigestUtils.md5Hex("BDUSS=" + bduss +
                "_client_version=8.1.0.4page_no=" + pageNum + "page_size=" + pageSize + "tiebaclient!!!");
        String body = "BDUSS=" + bduss + "&" + "_client_version=8.1.0.4&page_no=" + pageNum + "&page_size=" + pageSize + "&sign=" + md5Hex;
        return postClient("http://c.tieba.baidu.com/c/f/forum/like", header, body, cm).getResponseStr();
    }

    /**
     * 签到
     */
    public String signForums(String name, String id, String tbs, String bduss, PoolingHttpClientConnectionManager cm) throws Exception {
        String header = "Cookie=BDUSS=" + bduss + "&" +
                "Content-Type=application/x-www-form-urlencoded&Charset=UTF-8&net=3&" +
                "User-Agent=bdtb for Android 8.4.0.1&Connection=Keep-Alive&Accept-Encoding=gzip&" +
                "Host=c.tieba.baidu.com";
        String md5Hex = DigestUtils.md5Hex("BDUSS=" + bduss +
                "fid=" + id + "kw=" + name + "tbs=" + tbs + "tiebaclient!!!");
        String body = "BDUSS=" + bduss + "&fid=" + id + "&kw=" + name + "&sign=" + md5Hex + "&tbs=" + tbs;
        logger.info("发送签到信息:" + tbs);
        return postClient("http://c.tieba.baidu.com/c/c/forum/sign", header, body, cm).getResponseStr();
    }

    /**
     * VIP签到
     */
    public String vipSign(String tbs, String bduss, PoolingHttpClientConnectionManager cm) throws Exception {
        String firefoxHeader = "Content-Type: application/x-www-form-urlencoded\n" +
                "Charset: UTF-8\n" +
                "net: 3\n" +
                "User-Agent: bdtb for Android 8.4.0.1\n" +
                "Connection: Keep-Alive\n" +
                "Accept-Encoding: gzip\n" +
                "Host: c.tieba.baidu.com";
        firefoxHeader = firefoxHeader.replaceAll(": ", "=").replaceAll("\n", "&");
        String header = "Cookie=BDUSS=" + bduss + "&" +
                firefoxHeader;
        String body = "ie=utf-8&tbs=" + tbs;
        return postClient("http://tieba.baidu.com/tbmall/onekeySignin1", header, body, cm).getResponseStr();
    }
}
